//
//  TextInputBoxView.m
//  RuiHeApp
//
//  Created by Jun on 2022/11/5.
//

#import "TextInputBoxView.h"

@interface TextInputBoxView()

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) UITextField *textField;


@end

@implementation TextInputBoxView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    [self.layer setBorderWidth:0.5];
    [self.layer setBorderColor:FontColor(@"#999999").CGColor];
    [self.layer setCornerRadius:5];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setFont:kSYSTEMFONT(10)];
    [_titleLab setTextColor:FontColor(@"#999999")];
    [self addSubview:_titleLab];
    
    _textField = [[UITextField alloc] init];
    [_textField setFont:kSYSTEMFONT(14)];
    [_textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [_textField setAutocorrectionType:UITextAutocorrectionTypeNo];
    [self addSubview:_textField];
    
    _typeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [_typeBtn setBackgroundColor:UIColor.whiteColor];
    _typeBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [_typeBtn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    [self addSubview:_typeBtn];
    
    
    kWeakSelf;
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.mas_offset(5);
        make.height.mas_equalTo(15);
        make.right.mas_offset(-3);
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab);
        make.right.equalTo(weakSelf.typeBtn.mas_left).offset(-10);
        make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(2);
        make.bottom.mas_offset(-5);
    }];
    
    [_typeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(28, 28));
        make.right.mas_offset(-2);
        make.centerY.mas_offset(0);
    }];
    
    
//    [[_textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
//        self.model.content = x;
//    }];
    
//    [[RACSignal merge:@[self.textField.rac_textSignal, RACObserve(self.textField, text)]] subscribeNext:^(NSString* text){
//        weakSelf.model.content = x;
//    }];
    
    [[RACSignal merge:@[self.textField.rac_textSignal, RACObserve(self.textField, text)]] subscribeNext:^(NSString* text){
        if ([text length]>self.model.maxLen) {
            weakSelf.model.content = [text substringWithRange:NSMakeRange(0,weakSelf.model.maxLen)];
            [weakSelf.textField setText: weakSelf.model.content];
        }else{
            weakSelf.model.content = text;
        }
    }];
    
//    [[self.textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        if (self.model.inputType == TextInputPasswordType) {
//            [weakSelf.typeBtn setHidden:weakSelf.model.content.length == 0?YES:NO];
//        }
//    }];
    
//    RACChannelTo(self.model, content) = RACChannelTo(self.textField, text);
//    [self.textField.rac_textSignal subscribe:RACChannelTo(self.model, content)];
    
    [[_typeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf typeBtnAction];
    }];
}


- (void)typeBtnAction{
    if (self.typeBtnActionBlock) {
        self.typeBtnActionBlock(self.model);
    }
    
    if (self.model.inputType == TextInputPasswordType) {
        self.model.isSecure = !self.model.isSecure;
        [self setModel:self.model];
    }
}

- (void)setTitle:(NSString *)title{
    _title = title;
    [_titleLab setText:_title];
}

-(void)setDisable:(BOOL)disable
{
    [self.layer setBorderColor:disable?[UIColor colorWithHexString:@"999999" alpha:0.6].CGColor:[UIColor colorWithHexString:@"999999" alpha:1].CGColor];
    [_titleLab setTextColor:disable?[UIColor colorWithHexString:@"999999" alpha:0.6]:[UIColor colorWithHexString:@"999999" alpha:1]];
    _textField.textColor = disable?[UIColor colorWithHexString:@"000000" alpha:0.6]:[UIColor colorWithHexString:@"000000" alpha:1];
}

- (void)setModel:(TextInputModel *)model{
    _model = model;
    [_titleLab setText:_model.titile.rh_localized];
    [_titleLab setFont:[UIFont systemFontOfSize:_model.t_font]];
    [_titleLab setTextColor:FontColor(_model.t_color)];
    
    [_textField setText:_model.content.rh_localized];
    [_textField setFont:[UIFont systemFontOfSize:_model.c_font]];
    [_textField setTextColor:FontColor(_model.c_color)];
    [_textField setPlaceholder:_model.placeholder];
    [_textField setSecureTextEntry:_model.isSecure];
    if (_model.inputType == TextInputDeflutType) {//默认样式 仅输入
        [_textField setEnabled:YES];
        [_textField setKeyboardType:_model.keyboardType];
        [_typeBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        if (_model.titile.length == 0) {
            [_titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_offset(3);
                make.height.mas_equalTo(0);
            }];
        }
    }
    if (_model.inputType == TextInputChoiceType) {//仅下拉选择
        [_textField setEnabled:NO];
        if (_model.titile.length == 0) {
            [_typeBtn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
            [_typeBtn setBackgroundColor:UIColor.blackColor];
            [_typeBtn.layer setCornerRadius:5];
            [_titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_offset(3);
                make.height.mas_equalTo(0);
            }];
        }else{
            [_typeBtn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        }
    }
    if (_model.inputType == TextInputOrChoiceType) {//下拉选择+输入
        [_typeBtn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    }
    
    if (_model.inputType == TextInputOrScanType) {//扫码+输入
        [_typeBtn setImage:[UIImage imageNamed:@"intpu_scan_icon"] forState:UIControlStateNormal];
    }
    
    if (_model.inputType == TextInputPasswordType) {//密码输入
        if (_model.titile.length == 0) {
            [_titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_offset(3);
                make.height.mas_equalTo(0);
            }];
        }
        
        //[_typeBtn setHidden:self.model.content.length == 0 ? YES:NO];
        [_typeBtn setImage:[UIImage imageNamed:self.model.isSecure?@"password_unopen":@"password_open"] forState:UIControlStateNormal];
    }
    
    if (_model.inputType == TextInputunEnabledType) {//禁止编辑
        [_textField setEnabled:NO];
        [_typeBtn setImage:[UIImage imageNamed:self.model.isSecure?@"arrow_right_big":(self.model.isShowClear?@"intpu_scan_icon":@"")] forState:UIControlStateNormal];
    }
    
    //RAC(_model,content) = RACObserve(_textField,text);
}

@end
